package com.vallis.statis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.math.MathUtil;
import lombok.Data;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname LotteryUtil
 * @Description TODO
 * @Date 2020/12/3 1:43
 * @Created by vallis
 */
@Data
public class LotteryUtil {
    
    public  static  int current=0;
    public static  int amount=100;
    /**
     * 将输入的 String数组 转换成 int类型
     *
     * @param checkNumber
     *
     * @return
     */
    public static int[] stringArrayToInt(String[] checkNumber) {
        int[] checkNumberByInt = new int[checkNumber.length];
        for (int i = 0; i < checkNumber.length; i++) {
            checkNumberByInt[i] = Integer.parseInt(checkNumber[i]);
            
        }
        return checkNumberByInt;
    }
    /**
     * 将输入的 int数组 转换成 String类型
     */
    public static String[] intArrayToString(int[] checkNumber) {
        
        String[] checkNumberByStr = new String[checkNumber.length];
        for (int i = 0; i < checkNumber.length; i++) {
            checkNumberByStr[i] = String.valueOf(checkNumber[i]);
            
        }
        return checkNumberByStr;
        
    }
    /**
     * 如果包含中文逗号 一样处理
     * @param string
     * @return
     */
    public static String[] getString(String string){
        if (string.contains(",")){
            String[] split = string.split(",");
            return split;
        }else if (string.contains("，")){
            String[] split = string.split("，");
            return split;
        }else if (string.contains(" ")){
            String[] split = string.split(" ");
            return split;
        }
        return new String[]{string};
    }
    
    /**
     * 查询每个String[] 在 List<String[]>中的出现次数
     * @param c
     * @param o
     * @return
     */
    public static int frequency(List<String[]> c, String[] o) {
        int result = 0;
        if (o == null) {
            for (String[] e : c){
                if (Arrays.equals(e, o)){
                    result++;
                }
                
            }
            
        } else {
            for (String[] e : c) {
                if (Arrays.equals(e, o)) {
                    result++;
                }
            }
        }
        return result;
    }
    
    
    /**
     * 数组去重
     * @param lotteryTickets
     * @return
     */
    public  static    List<LotteryTicket> distinct(List<LotteryTicket> lotteryTickets) {
        
        Set<LotteryTicket>  set=new HashSet<>(lotteryTickets);
    
        List<LotteryTicket> temp=new ArrayList<>(set);
        
        return temp;
        
    
    
    }
    /**
     * 计算该实票的价格
     * @param
     * @return
     */
    public static Long getSinglePrice(String[] f, String[] e,int type) {
        if (type==1){
            long fp = MathUtil.combinationCount(f.length, 5);
            long ep = MathUtil.combinationCount(e.length, 2);
            return fp * ep * 2;
        }else {
            long fp = MathUtil.combinationCount(f.length, 6);
            long ep = MathUtil.combinationCount(e.length, 1);
            System.out.println("双色球价格计算：");
            System.out.println(fp);
            System.out.println(ep);
            return fp * ep * 2;
        }
      
    }
    /**
     * 将单注彩票分解成N个0+N
     *
     * @param superLotto
     *
     * @return
     */
    public static List<String[]> decomposedEnd(LotteryTicket superLotto, int n) {
       
        
        return MathUtil.combinationSelect(LotteryUtil.intArrayToString(superLotto.getEndNumArray()), n);
    }
    /**
     * 将单注彩票分解成N个N+0
     *
     * @param superLotto
     *
     * @return
     */
    public static List<String[]> decomposedFront(LotteryTicket superLotto, int n) {
        String[] checkNumber = LotteryUtil.intArrayToString(superLotto.getFrontNumArray());
        
        return MathUtil.combinationSelect(checkNumber, n);
    }
    /**
     * 将此大乐透彩票拆解成标准的5+2形式
     *
     * @param superLotto
     *
     * @return
     */
    public static List<LotteryTicket> decomposed(LotteryTicket superLotto) {
        List<String[]> allFist = MathUtil.combinationSelect(intArrayToString(superLotto.getFrontNumArray()), 5);
        List<String[]> allBack = MathUtil.combinationSelect(intArrayToString(superLotto.getEndNumArray()), 2);
        List<LotteryTicket> tickets = new ArrayList<>();
        LotteryTicket lottoTicket = null;
        for (String[] first : allFist) {
            for (String[] back : allBack) {
               StringBuilder firstString=new StringBuilder();
               StringBuilder endString=new StringBuilder();
                for (int i = 0; i < first.length; i++) {
                    if (i==first.length-1){
                        firstString.append(first[i]);
                    }else if (i<first.length-2){
                        firstString.append(first[i]+",");
                    }
                   
                }
                for (int i = 0; i < back.length; i++) {
                    if (i==back.length-1){
                        endString.append(back[i]);
                    }else if (i<back.length-2){
                        endString.append(back[i]+",");
                    }
        
                }
                lottoTicket = new LotteryTicket(firstString.toString(),endString.toString(),stringArrayToInt(first),stringArrayToInt(back));
                tickets.add(lottoTicket);
            }
        }
        return tickets;
    }
    
    
    /**
     * 将此大乐透彩票拆解成标准的6+1形式
     *
     * @param superLotto
     *
     * @return
     */
    public static List<LotteryTicket> decomposedDoubleBall(LotteryTicket superLotto) {
        List<String[]> allFist = MathUtil.combinationSelect(intArrayToString(superLotto.getFrontNumArray()), 6);
        List<String[]> allBack = MathUtil.combinationSelect(intArrayToString(superLotto.getEndNumArray()), 1);
        List<LotteryTicket> tickets = new ArrayList<>();
        LotteryTicket lottoTicket = null;
        
        for (String[] first : allFist) {
            for (String[] back : allBack) {
                StringBuilder firstString=new StringBuilder();
                StringBuilder endString=new StringBuilder();
                for (int i = 0; i < first.length; i++) {
                    if (i==first.length-1){
                        firstString.append(first[i]);
                    }else if (i<first.length-2){
                        firstString.append(first[i]+",");
                    }
                    
                }
                for (int i = 0; i < back.length; i++) {
                    if (i==back.length-1){
                        endString.append(back[i]);
                    }else if (i<back.length-2){
                        endString.append(back[i]+",");
                    }
                    
                }
                lottoTicket = new LotteryTicket(firstString.toString(),endString.toString(),stringArrayToInt(first),stringArrayToInt(back));
                tickets.add(lottoTicket);
            }
        }
        return tickets;
    }
    
    /**
     * 实际数据 N+0 或0+N
     * 票面数据：
     *      1+0：1
     *      2+0：2
     *      3+0：3
     *      4+0：4
     *      5+0：5
     *      6+0：6
     *      0+1：7
     *      0+2：8
     *
     
     * @return
     */
    public static List<LotteryOutPut> calcByTrue(List<LotteryTicket> data, int vType,int ticketType){
        
        //新建输入内容
        List<LotteryOutPut>  lotteryOutPuts=new ArrayList<>();
        //N+0集合或0+N
        List<String[]> typeList=new ArrayList<>();
        //去重用 n+0票
        List<LotteryTicket> typeTemp=new ArrayList<>();
        current=0;
        data.forEach(item->{
                    //分解成标准的5+2形式
            List<LotteryTicket> decomposed=null;
            if (ticketType==1){
                decomposed = LotteryUtil.decomposed(item);
            }else if(ticketType==2){
                decomposed = LotteryUtil.decomposedDoubleBall(item);
            }
                   
                  
                    decomposed.forEach(item2->{
                        if (vType<7){
                            
                            List<String[]> strings = LotteryUtil.decomposedFront(item2,vType);
                            typeList.addAll(strings);
                        }else if(vType==7){
                            List<String[]> strings = LotteryUtil.decomposedEnd(item2,1);
                            typeList.addAll(strings);
                        }else if (vType==8){
                            List<String[]> strings = LotteryUtil.decomposedEnd(item2,2);
                            typeList.addAll(strings);
                        }
                    });
                }
        );
       
        //此时typeList 已经装满，生成对应的LotteryTicket,目的是为了去重
        
        typeList.forEach(item->{
            LotteryTicket lotteryTicket=new LotteryTicket(Arrays.toString(item),Arrays.toString(item),LotteryUtil.stringArrayToInt(item),LotteryUtil.stringArrayToInt(item));
            //添加入一个临时集合
            typeTemp.add(lotteryTicket);
            
            
        });
        current=30;
        System.out.println("6+0前区大小"+typeTemp.size());
        List<LotteryTicket> distinct = LotteryUtil.distinct(typeTemp);
    
      
        //将临时集合中的每一个LotteryTicket 转换成输入内容
      
        distinct.forEach(item->{
            //将输出内容转换成String[]
            if (vType<7){
                String[] strings = LotteryUtil.intArrayToString(item.getFrontNumArray());
                //新建一个输出内容
                LotteryOutPut lotteryOutPut=new LotteryOutPut(strings);
                //计算出现的次数
                lotteryOutPut.setTimes(LotteryUtil.frequency(typeList,strings));
                lotteryOutPut.setType(vType);
                //添加至输出内容
                lotteryOutPuts.add(lotteryOutPut);
            }else if (vType==7){
                String[] strings = LotteryUtil.intArrayToString(item.getEndNumArray());
                //新建一个输出内容
                LotteryOutPut lotteryOutPut=new LotteryOutPut(strings);
                //计算出现的次数
                lotteryOutPut.setTimes(LotteryUtil.frequency(typeList,strings));
                lotteryOutPut.setType(vType);
                //添加至输出内容
                lotteryOutPuts.add(lotteryOutPut);
            }else if (vType==8){
                String[] strings = LotteryUtil.intArrayToString(item.getEndNumArray());
                //新建一个输出内容
                LotteryOutPut lotteryOutPut=new LotteryOutPut(strings);
                //计算出现的次数
                lotteryOutPut.setTimes(LotteryUtil.frequency(typeList,strings));
                lotteryOutPut.setType(vType);
                //添加至输出内容
                lotteryOutPuts.add(lotteryOutPut);
            }
            
            
        });
        current=70;
        System.out.println("6+0大小"+  lotteryOutPuts.size());
      
//        System.out.println("实际数据：");
//        System.out.println(lotteryOutPuts.size());
//        lotteryOutPuts.forEach(System.out::println);
        
        return lotteryOutPuts;
    }
    
    /**
     * 批量统计中奖信息
     * @param data
     * @param type
     * @return
     */
    public static List<LotteryTicket> calcAwardByList(List<LotteryTicket> data,LotteryTicket luckyNumber,int type){
    
        System.out.println(luckyNumber);
        //计算后的结果
        List<LotteryTicket> result=new ArrayList<>();
        data.forEach(item->{
        
            //计算每一张的中奖情况
            LotteryTicket lotteryTicket = calcAwardByOne(item, luckyNumber, type);
            Long singlePrice = LotteryUtil.getSinglePrice(LotteryUtil.intArrayToString(item.getFrontNumArray()), LotteryUtil.intArrayToString(item.getEndNumArray()), type);
      
            lotteryTicket.setPrice(singlePrice.intValue());
            result.add(lotteryTicket);
        
        });
    
       
    
    return result;
    
    }
    
    /**
     * 单张彩票中奖金额写入
     * @param data
     * @param type
     * @return
     */
    public static LotteryTicket calcAwardByOne(LotteryTicket data, LotteryTicket luckyNumber,int type){
       
        System.out.println("000000000"+luckyNumber);
        AwardReport awardReport=new AwardReport();
        AtomicInteger first= new AtomicInteger();
        AtomicInteger second= new AtomicInteger();
        AtomicInteger third= new AtomicInteger();
        AtomicInteger fourth= new AtomicInteger();
        AtomicInteger fifth= new AtomicInteger();
        AtomicInteger sixth= new AtomicInteger();
        AtomicInteger seven= new AtomicInteger();
        AtomicInteger eight= new AtomicInteger();
        AtomicInteger ninth= new AtomicInteger();
        AtomicInteger no= new AtomicInteger();
        if (type==1){
            List<LotteryTicket> decomposed = decomposed(data);
            
            decomposed.forEach(item->{
                LotteryTicket lotteryTicket = calcAwardBySingular(item,luckyNumber,type);
                //此处已计算出每一张票的获奖信息
                //判断并记录每张票的获奖信息，并且记录下
                switch (lotteryTicket.getAwardReport().getAwardType()){
                    case 1:
                        first.getAndIncrement();
                        break;
                    case 2:
                        second.getAndIncrement();
                        break;
                    case 3:
                        third.getAndIncrement();
                        break;
                    case 4:
                        fourth.getAndIncrement();
                        break;
                    case 5:
                        fifth.getAndIncrement();
                        break;
                    case 6:
                        sixth.getAndIncrement();
                        break;
                    case 7:
                        seven.getAndIncrement();
                        break;
                    case 8:
                        eight.getAndIncrement();
                        break;
                    case 9:
                        ninth.getAndIncrement();
                        break;
                    default:
                        no.getAndIncrement();
                }
                
                
                
                        
                        
            });
            awardReport.setFirst(first.get());
            awardReport.setSecond(second.get());
            awardReport.setThird(third.get());
            awardReport.setFourth(fourth.get());
            awardReport.setFifth(fifth.get());
            awardReport.setSixth(sixth.get());
            awardReport.setSeventh(seven.get());
            awardReport.setEighth(eight.get());
            awardReport.setNinth(ninth.get());
            awardReport.setNo(no.get());
            int i =             first.get()*1000000+second.get()*200000+third.get()*10000+fourth.get()*3000+fifth.get()*300+sixth.get()*200+seven.get()*100+eight.get()*15+ ninth.get() * 5;
            awardReport.setAwardMoney(i);
            
            
            
        }else if (type==2){
            List<LotteryTicket> tickets = decomposedDoubleBall(data);
            tickets.forEach(item->{
                LotteryTicket lotteryTicket = calcAwardBySingular(item,luckyNumber,type);
                //此处已计算出每一张票的获奖信息
                //判断并记录每张票的获奖信息，并且记录下
                switch (lotteryTicket.getAwardReport().getAwardType()){
                    case 1:
                        first.getAndIncrement();
                        break;
                    case 2:
                        second.getAndIncrement();
                        break;
                    case 3:
                        third.getAndIncrement();
                        break;
                    case 4:
                        fourth.getAndIncrement();
                        break;
                    case 5:
                        fifth.getAndIncrement();
                        break;
                    case 6:
                        sixth.getAndIncrement();
                        break;
                    
                    default:
                        no.getAndIncrement();
                }
        
        
        
        
        
            });
            awardReport.setFirst(first.get());
            awardReport.setSecond(second.get());
            awardReport.setThird(third.get());
            awardReport.setFourth(fourth.get());
            awardReport.setFifth(fifth.get());
            awardReport.setSixth(sixth.get());
         ;
            awardReport.setNo(no.get());
            int i =    first.get()*5000000+second.get()*300000+third.get()*3000+fourth.get()*200+fifth.get()*10+sixth.get()*5;
            awardReport.setAwardMoney(i);
    
    
    
        }
    
        data.setAwardReport(awardReport);
    return data;
    }
    
    /**
     * 单注彩票中奖信息写入
     * @param data
     * @param luckyNumber
     * @param type
     * @return
     */
    public static LotteryTicket calcAwardBySingular(LotteryTicket data, LotteryTicket luckyNumber, int type){
        System.out.println(luckyNumber);
        List<Integer> list = arrayToList(data.getFrontNumArray());
        List<Integer> list1 = arrayToList(luckyNumber.getFrontNumArray());
        List<Integer> list2 = arrayToList(data.getEndNumArray());
        List<Integer> list3 = arrayToList(luckyNumber.getEndNumArray());
      
    
            int frontTimes = CollUtil.intersection(list, list1).size();
            int endTimes = CollUtil.intersection(list2, list3).size();
        AwardReport awardName = getAwardName(frontTimes, endTimes, type);
        data.setAwardReport(awardName);
        data.setAward(awardName.getAwardMoney());
        
        return data;
        
    }
    
   
    
    public static void main(String[] args) {
        LotteryTicket d=new LotteryTicket("1,2,3,4,5,6,7,8","1,2",new int[]{1,2,3,4,5,6,7,8},new int[]{1,2});
        LotteryTicket x=new LotteryTicket("6,2,3,4,5","3,2",new int[]{6,2,3,4,5},new int[]{3,2});
      //  List<LotteryTicket> tickets = decomposed(d);
        List<LotteryTicket> tickets = decomposedDoubleBall(d);
        System.out.println(tickets.size());
       
    }
    
    /**
     * 数组转list
     * @param array
     * @return
     */
    public static List<Integer> arrayToList(int[] array){
        List<Integer> resultList = new ArrayList<>(array.length);
        for (Integer s : array) {
            resultList.add(s);
        }
        return resultList;
    }
    
    /**
     * 判断该注号码的中奖信息
     * @param ft
     * @param et
     * @param type
     * @return
     */
    public  static AwardReport getAwardName(int ft,int et,int type){
        AwardReport awardReport=new AwardReport();
    switch (type){
        case 1:
            awardReport.setTicketType(1);
            if (ft==5 &&et==2){
                awardReport.setFirst(1);
                awardReport.setContext("中的一等奖5+2");
                awardReport.setAwardType(1);
                awardReport.setAwardMoney(10000000);
                return awardReport;
            }else if (ft==5 &&et==1){
                awardReport.setSecond(1);
                awardReport.setContext("中的二等奖5+1");
                awardReport.setAwardType(2);
                awardReport.setAwardMoney(200000);
                return awardReport;
            }else if (ft==5 &&et==0){
                awardReport.setThird(1);
                awardReport.setContext("中的三等奖5+0");
                awardReport.setAwardType(3);
                awardReport.setAwardMoney(10000);
                return awardReport;
            }else if (ft==4 &&et==2){
                awardReport.setFourth(1);
                awardReport.setContext("中的四等奖4+2");
                awardReport.setAwardType(4);
                awardReport.setAwardMoney(3000);
                return awardReport;
            }else if (ft==4 &&et==1){
                awardReport.setFifth(1);
                awardReport.setContext("中的五等奖4+1");
                awardReport.setAwardType(5);
                awardReport.setAwardMoney(300);
                return awardReport;
            }else if (ft==3 &&et==2){
                awardReport.setSixth(1);
                awardReport.setContext("中的六等奖3+2");
                awardReport.setAwardType(6);
                awardReport.setAwardMoney(200);
                return awardReport;
            }else if (ft==4 &&et==0){
                awardReport.setSeventh(1);
                awardReport.setContext("中的七等奖4+0");
                awardReport.setAwardType(7);
                awardReport.setAwardMoney(100);
                return awardReport;
            }else if ((ft==3 &&et==1 )||(ft==2 &&et==2)){
                awardReport.setEighth(1);
                awardReport.setContext("中的八等奖"+ft+"+"+et);
                awardReport.setAwardType(8);
                awardReport.setAwardMoney(15);
                return awardReport;
            }else if ((ft==3 &&et==0)||(ft==2 &&et==1)||(ft==1 &&et==2)||(ft==0 &&et==2)){
                awardReport.setNinth(1);
                awardReport.setContext("中的九等奖"+ft+"+"+et);
                awardReport.setAwardType(9);
                awardReport.setAwardMoney(5);
                return awardReport;
            }
            awardReport.setContext("未中奖");
            awardReport.setAwardType(0);
            return awardReport;
        case 2:
            awardReport.setTicketType(2);
            if (ft==6 &&et==1){
                awardReport.setFirst(1);
                awardReport.setContext("中的一等奖"+ft+"+"+et);
                awardReport.setAwardType(1);
                awardReport.setAwardMoney(5000000);
                return awardReport;
            }else if (ft==6 &&et==0){
                awardReport.setSecond(1);
                awardReport.setContext("中的二等奖"+ft+"+"+et);
                awardReport.setAwardType(2);
                awardReport.setAwardMoney(200000);
                return awardReport;
            }else if (ft==5 &&et==1){
                awardReport.setThird(1);
                awardReport.setContext("中的三等奖"+ft+"+"+et);
                awardReport.setAwardType(3);
                awardReport.setAwardMoney(3000);
                return awardReport;
            }else if ((ft==5 &&et==0)||(ft==4&&et==1)){
                awardReport.setFourth(1);
                awardReport.setContext("中的四等奖"+ft+"+"+et);
                awardReport.setAwardType(4);
                awardReport.setAwardMoney(200);
                return awardReport;
            }else if ((ft==4 &&et==0)||(ft==3 &&et==1)){
                awardReport.setFifth(1);
                awardReport.setContext("中的五等奖"+ft+"+"+et);
                awardReport.setAwardType(5);
                awardReport.setAwardMoney(10);
                return awardReport;
            }else if ((ft==2 &&et==1)||(ft==1 &&et==1)||(ft==0 &&et==1)){
                awardReport.setSixth(1);
                awardReport.setContext("中的六等奖"+ft+"+"+et);
                awardReport.setAwardType(6);
                awardReport.setAwardMoney(5);
                return awardReport;
            }
            awardReport.setContext("未中奖");
            awardReport.setAwardType(0);
            awardReport.setAwardMoney(0);
            return awardReport;
          
        default:
            return awardReport;
          
    }
    
    }
}
